<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <el-form
        ref="searchMap"
        class="serach_wrapper flex-space-between"
        :inline="true"
        :model="searchMap">
          <div class="search">
            <el-form-item prop="timeRange" size="small">
              <el-date-picker v-model="searchMap.timeRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="timeChange"></el-date-picker>
            </el-form-item>
            <el-form-item prop="keyword" size="small">
              <el-input
                v-model="searchMap.keyword"
                placeholder="请输入受访人/预约人/同行人"
                clearable
                @keyup.enter.native="btn_primary"/>
            </el-form-item>
            <el-form-item class="flex">
              <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
              <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
            </el-form-item>
          </div>
          <div class="edit flex">
            <el-button    
              icon="el-icon-plus"
              class="btn_primary"
              @click="handleAdd()">
              新增
          </el-button>
          </div>
      </el-form>
      <div class="table_container">
        <el-table stripe v-loading="visitorLoading" :data="visitorList">
          <el-table-column type="index" label="序号" header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="visitedName" label="受访人" align="center"></el-table-column>
          <el-table-column prop="visitorName" label="预约人" align="center"></el-table-column>
          <el-table-column prop="projectPointName" label="同行人" align="center">
            <template slot-scope="scope">
              <span v-for="item in scope.row.entourageInfo" :key="item.entourageMobile">
                {{ item.entourageName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="预约时间" align="center"></el-table-column>
          <el-table-column prop="startTime" label="签到时间" align="center"></el-table-column>
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <span type="text" class="tag_primary" size="small" @click="handleDetails(scope.row)">详情</span>
              <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
            </template>
          </el-table-column>          
        </el-table>
      </div>
      <pagination v-if="total>0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getVisitorList"/>
    </div>
    <!-- 新增预约信息 -->
    <el-dialog :title="title" class="m_dialog" :visible.sync="dialogVisableVisitor" :close-on-click-modal="false">     
      <el-form ref="form" :model="formData" :rules="visitorRules" label-width="100px" size="small">
        <custom-title title="预约信息"/>
        <el-row>
          <el-col :span="12">
            <el-form-item label="受访人" prop="visitedName" size="small">
              <el-input v-model="formData.visitedName" placeholder="请输入角色名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约时间" prop="startTime" size="small">
              <el-date-picker
                
                v-model="formData.startTime"
                align="right"
                type="datetime"
                value-format= "yyyy-MM-dd HH:mm:ss"
                placeholder="预约到访时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <custom-title title="预约人员"/>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预约人" prop="visitorName" size="small">
              <el-input v-model="formData.visitorName" placeholder="请输入预约人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="visitorMobile" size="small">
              <el-input v-model="formData.visitorMobile" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="visitorIdcard" size="small">
              <el-input v-model="formData.visitorIdcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
        </el-row>
        <custom-title title="同行人员">
          <template #utils>
            <el-button class="btn_primary" @click="handleAddEntourage">新增</el-button>
          </template>
        </custom-title>
        <base-table :tableModel="formData.entourageInfo" :tableItemConfig="entourageInfoConfig"> 
          <template v-slot:entourageName="{ scope,item }">{{ scope.row[item.prop] }}</template>
          <template v-slot:entourageMobile="{ scope,item }">{{ scope.row[item.prop] }}</template>
          <template v-slot:entourageIdcard="{ scope,item }">{{ scope.row[item.prop] }}</template>
        </base-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisableVisitor = false" class="btn_cancel">取 消</el-button>
        <el-button type="primary" class="btn_primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog> 
    <!-- 同行人员弹框 -->
    <el-dialog :title="title" class="m_dialog" :visible.sync="dialogVisibleEntourage" :close-on-click-modal="false">     
      <el-form ref="entourageForm" :model="entourageModel" :rules="entourageRules" label-width="100px" size="small">
        <!--  -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="姓名" prop="entourageName" size="small">
              <el-input v-model="entourageModel.entourageName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="entourageMobile" size="small">
              <el-input v-model="entourageModel.entourageMobile" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证号" prop="entourageIdcard" size="small">
              <el-input v-model="entourageModel.entourageIdcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleEntourage = false" class="btn_cancel">取 消</el-button>
          <el-button @click="submitEntoruageForm" type="primary" class="btn_primary">确 认</el-button>
        </div>
    </el-dialog>
    <!-- 访客详情弹框 -->
    <el-dialog title="预约信息" class="m_dialog" :visible.sync="dialogVisibleDetails" :close-on-click-modal="false">     
        <!--  -->
        <el-form ref="form" :model="formData" label-width="120px" size="small">
          <custom-title title="任务详细">
            <template #utils>
              <el-button class="btn_primary" @click="dialogVisableVisitor = true">编辑</el-button>
            </template>
          </custom-title>
          <el-row>
            <el-col :span="12">
              <el-form-item label="受访人"> {{ formData.visitedName }} </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预约时间"> {{ formData.startTime }} </el-form-item>
            </el-col>
          </el-row>
          <custom-title title="预约人员"></custom-title>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预约人"> {{ formData.visitorName }} </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话"> {{ formData.visitorMobile }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="身份证号码"> {{ formData.visitorIdcard }} </el-form-item>
            </el-col>
          </el-row>
          <custom-title title="同行人员"></custom-title>
          <base-table :tableModel="formData.entourageInfo" :tableItemConfig="entourageInfoConfig"> 
            <template v-slot:entourageName="{ scope,item }">{{ scope.row[item.prop] }}</template>
            <template v-slot:entourageMobile="{ scope,item }">{{ scope.row[item.prop] }}</template>
            <template v-slot:entourageIdcard="{ scope,item }">{{ scope.row[item.prop] }}</template>
        </base-table>
        </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listVisitor,addVisitor,getVisitorById,deleteVisitor,updateVisitor } from '@/api/visitor'

export default {
  data() {
    var checkIdCard = (rule, value, callback) => {
      const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (!value) {
          callback(new Error('请输入身份证号'));
        } else if (!idCardRegex.test(value)) {
          callback(new Error('身份证号格式不正确'));
        } else {
          callback();
            }
          }
    return {
      // 访客预约搜索数据
      searchMap: {  
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        keyword: undefined,
        startTime: undefined,
        endTime: undefined,
        timeRange: undefined
      },
      // 访客预约数据
      formData: {},
      // 访客预约表单验证
      visitorRules: {
        timeRange: [{ required: true, message: '请选择日期', trigger: 'change'}],
        visitedName: [{ required: true, message: '请输入受访人', trigger: 'blur' }],
        visitorName: [{ required: true, message: '请输入预约人', trigger: 'blur' }],
        visitorMobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不正确',trigger: 'blur' }
        ],
        visitorIdcard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          { validator: checkIdCard, trigger: 'blur' }
        ]
      },
      // 预约列表加载状态
      visitorLoading: false,
      // 访客预约列表
      visitorList: [],
      // 访客预约条数
      total: 0,
      // 访客预约弹框
      dialogVisableVisitor: false,
      // 标题
      title: '',
      // 同行人员表格
      entourageInfoConfig: [
        {
          label: '姓名',
          prop: 'entourageName',
          slotName: 'entourageName'
        },
        {
          label: '电话',
          prop: 'entourageMobile',
          slotName: 'entourageMobile'
        },
        {
          label: '身份证',
          prop: 'entourageIdcard',
          slotName: 'entourageIdcard'
        }
      ],
      // 同行人员弹框
      dialogVisibleEntourage: false,
      entourageModel: {
        entourageName: '',
        entourageMobile: '',
        entourageIdcard: ''
      },
      // 同行表单验证
      entourageRules: {
        entourageName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        entourageMobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不正确',trigger: 'blur' }
        ],
        entourageIdcard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          { validator: checkIdCard, trigger: 'blur' }
        ]
      },
      // 访客详情弹框
      dialogVisibleDetails: false,
          // 描述配置
      descConfig: {
          size: 'medium',
          column: '2',
      },
      // 详情页配置项
      descConfigItem: [
        {
          fieIdName: 'visitedName',
          label: '受访人',
          span: '3',
        },
        {
          fieIdName: 'startTime',
          label: '预约时间',
          span: '3'
        },
        {
          fieIdName: 'visitorName',
          label: '预约人',
          span: '2'
        },
        {
          fieIdName: 'visitorMobile',
          label: '联系电话',
          span: '2'
        },
        {
          fieIdName: 'visitorIdcard',
          label: '身份证号码',
          span: '3'
        }
      ]
    };
  },
  created() {
    this.getVisitorList();
  },
  computed: {
    pageTitle() {
      return '访客预约查询'
    }
  },
  watch: {},
  methods: {
    getVisitorList() {
      this.visitorLoading = true;
      listVisitor(this.searchMap).then(res=> {
        console.log(res)
        if(200 === res.code) {
          this.visitorList = res.rows
          this.total = res.total 
          this.visitorLoading = false
        }
      })
    },
    // 点击访客详情
    handleDetails(row) {
      const visitorId = row.visitorId
      getVisitorById(visitorId).then(res=> {
        this.formData = res.data
        this.dialogVisibleDetails = true
      })
    },
    // 点击删除访客预约
    handleDelete(row) {
      const visitorId = [row.visitorId]
      this.$confirm('是否确认编号为"' + visitorId + '"的数据项？').then(res=>{
        return deleteVisitor(visitorId)
      }).then(()=>{
        this.getVisitorList()
        this.$message.success('删除成功')
      })
    },
    // 点击新增访客预约
    handleAdd() {
      this.reset();
      this.dialogVisableVisitor = true
      this.title = '新增访客预约'
    }, 
    reset() {
      this.formData = {
        startTime: '',
        // 	string($date-time) 预约到访时间
        type: '0',
        // 访问类型 0：主动访问 1:被邀约访问
        visitedName: '',
        // 被拜访人姓名
        visitorIdcard: '',
        // 访客身份证号
        visitorInviteId: '',
        // 访客邀请id
        visitorMobile: '',
        // 访客手机号
        visitorName: '',
        // 访客姓名
        entourageInfo: []
        // 同行人信息 (数组，每个对象包含姓名entourageName 和 电话entourageMobile) 和身份证 entourageIdcard)
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    // 点击添加同行人
    handleAddEntourage() {
      this.dialogVisibleEntourage = true
      this.entourageModel = {
        entourageName: '',
        entourageMobile: '',
        entourageIdcard: ''
      }
      this.$nextTick(() => {
        this.$refs['entourageForm'].clearValidate()
      })
    },
    // 随行人员确认
    submitEntoruageForm() {
      this.$refs['entourageForm'].validate((valid)=>{
        if(valid) {
          this.$set(this.formData.entourageInfo, this.formData.entourageInfo.length, this.entourageModel)
          this.dialogVisibleEntourage = false
        }
      })
    },
    // 提交
    submitForm() {
        this.$refs['form'].validate((valid)=>{
        if(valid) {
          if(this.formData.visitorId !== undefined) {
            updateVisitor(this.formData).then(res=>{
              if(200 === res.code) {
                this.$message.success('修改成功')
                this.dialogVisableVisitor = false
                this.getVisitorList()
              }
            })
          }else {
            addVisitor(this.formData).then(res=>{
              if(200 === res.code) {
                this.$message.success('预约成功')
                this.dialogVisableVisitor = false
              }
              this.getVisitorList()
            })
          }
      
        }
      })
    },
    handleSearch() {
      this.getVisitorList()
    },
    handleReset() {
      this.$refs.searchMap.resetFields()
      this.searchMap.startTime = ''
      this.searchMap.endTime = ''
      this.getVisitorList()
    },
    timeChange(val){
      console.log(val)
      this.searchMap.startTime = val[0]
      this.searchMap.endTime = val[1]
    }
  },
}
</script>
<style lang='less' scoped>

</style>